<?php
if(PERMISSION != "F")
{
	redirectPage("index.php?module=logout");
	exit();
}
$game_id = $db_mysql->db_input(setVar("id", ""));
if($game_id != "")
{
	$sql = "SELECT games.*, users.* FROM games LEFT JOIN users ON games.user_id = users.user_id WHERE games.game_id = '$game_id'";
	$fetch_array = true;
	include(DIR_WS_USER_INCLUDES."create_variable_value.php");
}
if(!(isset($user_id) && $user_id != ""))
{
	redirectPage("index.php?module=games_list");
}
$arr_legend = json_decode($crossword_legend, true);
$game_words = grid_to_words($crossword_grid);
$user_words = grid_to_words($user_word_grid);

$arr_correct_index = array();
for($i = 0; $i < count($user_words) ; $i++)
{
	if(strtolower($user_words[$i]) === strtolower($game_words[$i]))
	{
		$arr_correct_index[] = $i;
	}
}
?>
<?php include('includes/header.php'); ?>

<div id="content">
	<div id="stage-2" style="display: block">
		<?php include('includes/header_games.php'); ?>

		<div class="clear"></div>
		<br/>
		<div style="float:left;width:120px">WWID:</div><div><?=$unique_id?></div>
		<div style="float:left;width:120px">Full name:</div><div>&nbsp;<?=$full_name?></div>
		<div style="float:left;width:120px">Correct words:</div><div><?=$user_word_correct?></div>
		<div style="float:left;width:120px">Game time:</div><div><?=seconds_to_time($user_time)?></div>
		<div style="float:left;width:120px">Started at:</div><div><?=  format_date($start_time, "d/m/Y h:i:s")?></div>
		<div style="float:left;width:120px">Sent at:</div><div><?=  format_date($end_time, "d/m/Y h:i:s")?></div>
		<div style="float:left;width:120px">IP:</div><div><?=  $ip_address?></div>
		<div style="float:left;width:120px">Device:</div><div><?=  $from_device?></div>
		<div style="float:left;width:120px">Browser:</div><div><?=  getUserAgent($client_info)?></div>
		<div style="clear: both">&nbsp;</div>
		<h3>Result</h3>
		<div id="crossword">
			<div style="float:left;width:50%">Key</div>
			<div style="float:left;width:50%">Player</div>
			<div style="float:left;width:50%"></div>
			<div style="clear: both">&nbsp;</div>
			<div style="float:left;width:50%">
			<table class='key crossword'>
				<?php
				$arr_grid = json_decode($crossword_grid,true);
				for($i = 0; $i < count($arr_grid); $i++)
				{
					$row = $arr_grid[$i];
				?>
				<tr>
				<?php
				for($j = 0; $j < count($row); $j++)
				{
					$cell = $row[$j];

					if(is_array($cell))//has char
					{
						$is_start_of_word = 0;
						$index = 0;
						$position = "";
						$char = $cell["char"];

						$across = $cell["across"];
						if(is_array($across))
						{
							$is_start_of_word = $across["is_start_of_word"] ? 1 : 0;
							if($is_start_of_word == 1)
							{
								$position = get_position_by_index($arr_legend, $across["index"], "across");
							}
						}

						$down = $cell["down"];
						if(is_array($down) && $is_start_of_word == 0)
						{
							$is_start_of_word = $down["is_start_of_word"];
							if($is_start_of_word == 1)
							{
								$position = get_position_by_index($arr_legend, $down["index"], "down");
							}
						}
						$style = $position ? "background-image:url('https://crosswordlabs.com/media/img/numbers/".($position).".png');" : "";
						?>
					<td style="<?=$style?>" title="<?=$i?>, <?=$j?>" class=""><?=$char?></td>
						<?
					}
					else//no char
					{
					?>
					<td title="<?=$i?>, <?=$j?>" class="no-border">
					&nbsp;
					</td>
					<?
					}
				}
				?>
				</tr>
				<?	
				}
				?>
			</table>
			</div>
			<div style="float:left;width:50%">
			<table class='user crossword'>
				<?php
				$arr_grid = json_decode($user_word_grid,true);
				for($i = 0; $i < count($arr_grid); $i++)
				{
					$row = $arr_grid[$i];
				?>
				<tr>
				<?php
				for($j = 0; $j < count($row); $j++)
				{
					$cell = $row[$j];

					if(is_array($cell))//has char
					{
						$is_start_of_word = 0;
						$index = 0;
						$class_across = 0;
						$class_down = 0;
						$position = "";
						$char = $cell["char"];

						$across = $cell["across"];
						if(is_array($across))
						{
							$index = $across["index"];
							$class_across = in_array($index, $arr_correct_index) ? 1 : 0;
							
							$is_start_of_word = $across["is_start_of_word"] ? 1 : 0;
							if($is_start_of_word == 1)
							{
								$position = get_position_by_index($arr_legend, $across["index"], "across");
							}
						}

						$down = $cell["down"];
						if(is_array($down))
						{
							$index = $down["index"];
							$class_down = in_array($index, $arr_correct_index) ? 1 : 0;
							
							if($is_start_of_word == 0)
							{
								$is_start_of_word = $down["is_start_of_word"];
								if($is_start_of_word == 1)
								{
									$position = get_position_by_index($arr_legend, $down["index"], "down");
								}
							}
						}
						$style = $position ? "background-image:url('https://crosswordlabs.com/media/img/numbers/".($position).".png');" : "";
						?>
					<td style="<?=$style?>" title="<?=$i?>, <?=$j?>" class="<?=($class_across == 1 || $class_down == 1) ? "correct" : "wrong"?>"><?=$char?></td>
						<?
					}
					else//no char
					{
					?>
					<td title="<?=$i?>, <?=$j?>" class="no-border">
					&nbsp;
					</td>
					<?
					}
				}
				?>
				</tr>
				<?	
				}
				?>
			</table>
			</div>
		</div>
		<div style="clear: both">&nbsp;</div>
		<div id="clues">
			<div id="clues-inner">    
				<div id="across-box">
					<h5>Across</h5>
					<ol id="across">
						<?php
						foreach($arr_legend["across"] as $word)
						{
							$position = $word["position"];
							$index = $word["index"];
							$clue = $word["clue"];
						?>
						<li id="clue-<?=$index?>"><strong><?=$position?>.</strong> <?=$clue?></li>
						<?php
						}
						?>
					</ol>
				</div>
				<div id="down-box">        
					<h5>Down</h5>
					<ol id="down">
						<?php
						foreach($arr_legend["down"] as $word)
						{
							$position = $word["position"];
							$index = $word["index"];
							$clue = $word["clue"];
						?>
						<li id="clue-<?=$index?>"><strong><?=$position?>.</strong> <?=$clue?></li>
						<?php
						}
						?>
					</ol>
				</div>
			</div>
		</div>
		<div class="clear"></div>
	
    </div>
    </div>
</div>

